package com.dd.web.base.controller;

import com.dd.common.entity.CommonResult;
import com.dd.framework.security.jwt.JwtTokenHelper;
import com.dd.framework.security.jwt.property.JwtTokenConfig;
import com.dd.web.base.entity.view.UmsAdminLoginParam;
import com.dd.web.base.service.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "UmsLoginController", description = "登录")
@EnableConfigurationProperties(JwtTokenConfig.class)
@RequestMapping("/base/")
public class UmsLoginController {

    @Autowired
    private JwtTokenConfig jwtTokenConfig;
    @Autowired
    private UmsAdminService adminService;

    @Autowired
    private JwtTokenHelper jwtTokenHelper;

    @ApiOperation(value = "登录")
    @PostMapping(value = "/login")
    public CommonResult<Map> login(@RequestBody UmsAdminLoginParam umsAdminLoginParam) {
        String token = adminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());

        jwtTokenConfig.getTokenHeadKey();

        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", jwtTokenHelper.getJwtTokenConfig().getTokenHead());
        return CommonResult.success(tokenMap);
    }
}
